package com._1.message.elasticsearch;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Service;
import org.springframework.data.elasticsearch.core.document.Document;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

//    public void save(ProductInfo productInfo) {
//        ProductInfo save = productInfoRepository.save(productInfo);
//        System.out.println(save.getId());
//    }

    public void add(ProductInfo productInfo) {
        IndexCoordinates indexCoordinates = IndexCoordinates.of("product_info"); // 指定索引名称
        // 保存 ProductInfo 对象到 Elasticsearch
        ProductInfo indexedObjectInformation = elasticsearchRestTemplate.save(productInfo, indexCoordinates);
        System.out.println(indexedObjectInformation.getId());
    }

    public Iterable<ProductInfo> findAll() {
        return productInfoRepository.findAll();
    }

    public Optional<ProductInfo> search(String keyword) {
        // 构造模糊查询条件
        Criteria criteria = new Criteria("productName").contains(keyword);
        Query query = new CriteriaQuery(criteria);

        // 执行查询
        SearchHits<ProductInfo> search = elasticsearchRestTemplate.search(query, ProductInfo.class);

        // 检查是否有结果
        if (search.getSearchHits().isEmpty()) {
            return Optional.empty();
        }

        // 返回第一个结果
        return Optional.ofNullable(search.getSearchHit(0).getContent());
    }


    public Page<ProductInfo> searchByPage(String keyword, int page, int size) {
        // 设置分页参数
        Pageable pageable = PageRequest.of(page, size);

        // 构造模糊查询条件
        Criteria criteria = new Criteria("productName").contains(keyword);
        Query query = new CriteriaQuery(criteria).setPageable(pageable);

        // 执行查询
        SearchHits<ProductInfo> searchHits = elasticsearchRestTemplate.search(query, ProductInfo.class);

        // 将 SearchHits 转换为 Page 对象
        List<ProductInfo> content = searchHits.getSearchHits().stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());

        // 执行分页查询
        // 创建 Page 对象
        return new PageImpl<>(content, pageable, searchHits.getTotalHits());
    }
}
